package com.shell.student;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

//共享数据，com.shell.student
public class MyContentProvider extends ContentProvider {

    private DBHelper dbHelper;
    private SQLiteDatabase db;
    private static final int MULTIPLE_STUDENT = 1;//多名学生
    private static final int SINGLE_STUDENT = 2;//单个学生
    private static final UriMatcher uriMatcher;

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(DBUtil.AUTHORITY, DBUtil.PATH_MULTIPLE, MULTIPLE_STUDENT);
        uriMatcher.addURI(DBUtil.AUTHORITY, DBUtil.PATH_SINGLE, SINGLE_STUDENT);
    }

    public MyContentProvider() {
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        dbHelper = new DBHelper(getContext());
        db = dbHelper.getWritableDatabase();
        //判断数据库对象是否存在
        return db != null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        switch (uriMatcher.match(uri)) {
            case MULTIPLE_STUDENT:
                return db.delete(DBUtil.TABLE_NAME,selection,selectionArgs);//删除单个学生
//                return db.delete(DBUtil.TABLE_NAME,null,null);  //删除多名学生
            case SINGLE_STUDENT:


            default:
                throw new UnsupportedOperationException("Not yet implemented");//URI不正确
        }
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        Uri newUri = null;
        long newId=0;

        switch (uriMatcher.match(uri)) {
            case MULTIPLE_STUDENT:
               newId= db.insert(DBUtil.TABLE_NAME,null,values);
               if (newId>0){
                   newUri= ContentUris.withAppendedId(DBUtil.CONTENT_URI,newId);
               }
            case SINGLE_STUDENT:

                break;
            default:
                throw new UnsupportedOperationException("Not yet implemented");//URI不正确
        }
        return newUri;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        switch (uriMatcher.match(uri)) {
            case MULTIPLE_STUDENT:
                return db.query(DBUtil.TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
            case SINGLE_STUDENT:

                break;
            default:
                throw new UnsupportedOperationException("Not yet implemented");//URI不正确
        }
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        switch (uriMatcher.match(uri)) {
            case MULTIPLE_STUDENT:
                return db.update(DBUtil.TABLE_NAME,values,selection,selectionArgs);
            case SINGLE_STUDENT:

                break;
            default:
                throw new UnsupportedOperationException("Not yet implemented");//URI不正确
        }
        return 0;
    }
}